/*
 * 版权：Copyright PING AN INSURANCE (GROUP) COMPANY OF CHINA ,LTD. All Rights Reserved.
 * 项目名称：cloud-ucp-im-admin
 * 创建人： XIWEICHENG497
 * 创建时间：2018 下午3:30:58
 */
package com.xiweicheng.test.springboot.im.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiweicheng.test.springboot.im.admin.component.JwtTokenUtil;
import com.xiweicheng.test.springboot.im.admin.entity.security.User;
import com.xiweicheng.test.springboot.im.admin.entity.security.UserAuthority;
import com.xiweicheng.test.springboot.im.admin.ex.BusinessException;
import com.xiweicheng.test.springboot.im.admin.repository.AuthorityRepository;
import com.xiweicheng.test.springboot.im.admin.repository.UserRepository;
import com.xiweicheng.test.springboot.im.admin.service.UserService;

/**
 * TODO〈功能描述〉
 * 
 * @author XIWEICHENG497
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    UserDetailsService userDetailsService;

    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @Autowired
    UserRepository userRepository;

    @Autowired
    AuthorityRepository authorityRepository;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public String login(String username, String password) {

        UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(username, password);
        Authentication authentication = authenticationManager.authenticate(upToken);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        return jwtTokenUtil.generateToken(userDetails);
    }

    @Override
    public User register(User user) {

        String username = user.getUsername();
        if (userRepository.findByUsername(username) != null) {
            throw new BusinessException("用户已存在");
        }

        user.setPassword(passwordEncoder.encode(user.getPassword()));

        User user2 = userRepository.saveAndFlush(user);

        UserAuthority authority = new UserAuthority();
        authority.setAuthority("ROLE_USER");
        authority.setUser(user2);

        UserAuthority authority2 = authorityRepository.saveAndFlush(authority);

        user2.getAuthorities().add(authority2);

        return user2;
    }

    @Override
    public String refreshToken(String oldToken) {

        String token = oldToken.substring("Bearer ".length());
        if (!jwtTokenUtil.isTokenExpired(token)) {
            return jwtTokenUtil.refreshToken(token);
        }
        throw new BusinessException("Token过期");
    }

    @Override
    public User getInfo(String username) {

        return userRepository.findByUsername(username);
    }

}